﻿#if UNITY_EDITOR

// #define EditorGUIUtility_DEBUG

using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    public static class EditorGUIUtility {

        private static string s_filePath;
        
        public static T LoadAsset<T> (string fileName) where T : UnityEngine.Object {
            return AssetDatabase.LoadAssetAtPath<T> (GetTexturePath (fileName));
        }


        static string GetTexturePath (string textureName) {
            // Get the path to all files.
            // We need to do that, because the user maybe used a different location.
            if (string.IsNullOrEmpty (s_filePath)) {
                string[] res = Directory.GetFiles (Application.dataPath, "EditorGUIUtility.cs", SearchOption.AllDirectories);
                if (res.Length == 0) {
                    Debug.LogError ("EditorGUIUtility.cs could not be found.");
                    return null;
                }

                string absoluteClassPath = res[0].Replace ("EditorGUIUtility.cs", "").Replace ("\\", "/");
                int relativeIndex = absoluteClassPath.IndexOf ("Assets/");
                absoluteClassPath = absoluteClassPath.Substring (relativeIndex, absoluteClassPath.Length - relativeIndex);
                absoluteClassPath = absoluteClassPath.Replace ("Util/", "");
                absoluteClassPath = absoluteClassPath.Replace ("Scripts/", "");
                s_filePath = absoluteClassPath;
                
                #if EditorGUIUtility_DEBUG
                Debug.Log ("File Path found: " + s_filePath);
                #endif
            }

//            return "Assets/AI Graph/AI/Core/Editor/" + textureName;
            return s_filePath + textureName;
        }


        public static void RepaintEditorsOfType<T> (bool inherit = false) where T : UnityEditor.Editor {
            RepaintEditorsOfType (typeof (T), inherit);
        }


        public static void RepaintEditorsOfType (Type type, bool inherit = false) {
            UnityEditor.Editor[] editors = Resources.FindObjectsOfTypeAll<UnityEditor.Editor> ();

            foreach (UnityEditor.Editor editor in editors) {
                if (editor.GetType () == type || inherit && editor.GetType ().IsSubclassOf (type)) {
                    editor.Repaint ();
                }
            }
        }


        public static bool IsEditorWindowDocked (this EditorWindow window) {
            BindingFlags fullBinding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

            MethodInfo isDockedMethod = typeof (EditorWindow).GetProperty ("docked", fullBinding).GetGetMethod (true);

            return (bool) isDockedMethod.Invoke (window, null);
        }


        public static float CalculateTextWidth (this GUIStyle style, string text) {
            return style.CalcSize (new GUIContent (text)).x;
        }


        public static float CalculateTextHeight (this GUIStyle style, string text) {
            return style.CalcSize (new GUIContent (text)).y;
        }


        public static bool IsVerticalScrollbarEnabled (Rect scrollRect, Rect viewRect) {
            return viewRect.height > scrollRect.height;
        }


        public static bool IsHorizontalScrollbarEnabled (Rect scrollRect, Rect viewRect) {
            return viewRect.width > scrollRect.width;
        }

    }


}
#endif